from Tkinter import *
import random
import sys
import sha
import math

class App:

    def __init__(self, master):

        frame = Frame(master)
        frame.pack(padx=10, pady=10)

        self.useNumbers = BooleanVar(master, True, None)
        self.useSymbols = BooleanVar(master, True, None)

        # Label
        Label(frame, state=ACTIVE, text="Unencrypted Password: ").grid(row=0, padx=5, pady=5, sticky=W)
        
        # Text field
        self.input = Entry(frame, width=32, show="*")
        self.input.grid(row=0, column=1, padx=5, pady=5, sticky=W)
        # self.input.bind("<Key>", self.encryptListener)
        self.input.bind("<Return>", self.encryptListener)
        
        # Button
        self.submit = Button(frame, text="Encrypt", command=self.encrypt)
        self.submit.grid(row=0, column=2, sticky=W, padx=5, pady=5)
        
        # Label
        Label(frame, state=ACTIVE, text="Options: ").grid(row=1, padx=5, pady=5, sticky=E)
        
        # Slider
        self.slider = Scale(frame, from_=8, to=40, orient=HORIZONTAL, length=200, command=self.encryptListener)
        self.slider.grid(row=1, column=1, columnspan=2, sticky=W, padx=1, pady=5)
        sliderDefault = IntVar()
        sliderDefault.set(40)
        self.slider.config(variable=sliderDefault)
        
        # Checkbox: numbers
        self.useNumbersCheckbox = Checkbutton(frame, text="Use numbers in encryption", variable=self.useNumbers, command=self.encrypt, onvalue=True, offvalue=False)
        self.useNumbersCheckbox.select()
        self.useNumbersCheckbox.grid(row=2, column=1, columnspan=2, sticky=W)
        
        # Checkbox: symbols
        self.useSymbolsCheckbox = Checkbutton(frame, text="Use symbols in encryption", variable=self.useSymbols, command=self.encrypt, onvalue=True, offvalue=False)
        self.useSymbolsCheckbox.select()
        self.useSymbolsCheckbox.grid(row=3, column=1, columnspan=2, sticky=W)
        
        # Label
        Label(frame, state=ACTIVE, text="Encrypted Password: ").grid(row=4, padx=5, pady=5, sticky=W)
        
        # Results
        self.output = Entry(frame, state="readonly", relief="flat", width=45)
        self.output.grid(row=4, column=1, columnspan=2, padx=5, pady=5, sticky=W)

    def encryptListener(self, event):
        self.encrypt()

    def encrypt(self):
        
        result = ""
        
        if (len(self.input.get()) > 0):
            
            length = self.slider.get()
            crypt = sha.new(self.input.get())
            random.seed(int(crypt.hexdigest(), 16) / length)
            
            # Define possible characters
            
            chars = []
                
            for i in range(65, 91):
                chars.append(chr(i)) # A-Z
            
            for i in range(97, 123):
                chars.append(chr(i)) # a-z
            
            if self.useNumbers.get():
                for i in range(48, 58):
                    chars.append(chr(i)) # 0-9
            
            if self.useSymbols.get():
                for i in range(0,3):
                    chars.append("!");
                    chars.append("$");
                    chars.append("?");
            
            # Generate the string
            
            for i in range(0, length):                
                result = result + chars[random.randint(0, len(chars)-1)]
            
            
        var = StringVar()
        var.set(result)
        self.output.config(textvariable=var)
            

root = Tk()
app = App(root)
app.input.focus()
root.resizable(0, 0)
root.title("Patrick's Passwordifier")
root.mainloop()